package com.kh.first.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kh.first.pojo.ComputerApplyPojo;
import com.kh.first.pojo.PngPojo;
import com.kh.first.pojo.common.AjaxJson;
import com.kh.first.service.ComputerApplyService;
import com.kh.first.utils.UserUtils;

import lombok.Cleanup;

@Controller
@RequestMapping("computerApplyController")
public class ComputerApplyController {

	@Autowired
	ComputerApplyService computerApplyService;

	@RequestMapping(params = "doApply")
	@ResponseBody
	public AjaxJson<String> doApply(ComputerApplyPojo computerApplyPojo) {
		AjaxJson<String> ajaxJson = new AjaxJson<String>();
		String userPinYin = UserUtils.getUserPinYin();
		String userName = UserUtils.getUserName();
		computerApplyPojo.setApplyUserId(userPinYin);
		computerApplyPojo.setApplyUserName(userName);
		Long res = computerApplyService.saveComputerApply(computerApplyPojo);
		return ajaxJson;
	}

	@RequestMapping(params = "myApplyList") // userId必发
	public AjaxJson<ComputerApplyPojo> myApplyList(ComputerApplyPojo computerApplyPojo) {
		AjaxJson<ComputerApplyPojo> ajaxJson = new AjaxJson<ComputerApplyPojo>();
		List<ComputerApplyPojo> computerApplyList = computerApplyService.getComputerApplyList(computerApplyPojo);
		ajaxJson.list = computerApplyList;
		return ajaxJson;
	}

	@Value("${upload.image.sign.path}")
	String imagePath;

	@RequestMapping(params = "savePng")
	@ResponseBody
	public AjaxJson<String> savePng(PngPojo pngPojo) throws IOException {
		// 1.自己
		// 2.部门经理
		// 3.信息中心审核员
		// 4.信息中心主任
		// 5.采购经理
		// 6.采购副总
		AjaxJson<String> ajaxJson = new AjaxJson<String>();
		Long id = pngPojo.getId();
		Integer signNo = pngPojo.getSignNo();
		if (null == signNo || null == id) {
			ajaxJson.data = "程序有问题，请联系管理员";
			ajaxJson.code = AjaxJson.error;
			return ajaxJson;
		}
		byte[] b64Png = Base64.decodeBase64(pngPojo.getPngReceive().replace("data:image/png;base64,", ""));
		String uuid = UUID.randomUUID().toString().replace("-", "");
		String savedFileName = uuid + ".png";
		String pathname = imagePath + File.separator + savedFileName;
		ComputerApplyPojo computerApplyPojo = new ComputerApplyPojo();
		computerApplyPojo.setId(id);

		switch (signNo) {
		case 1:
			computerApplyPojo.setApplyUserSignImgUrl(savedFileName);
			computerApplyPojo.setApplyTime(new Date());
			break;
		case 2:
			computerApplyPojo.setALSignImgUrl(savedFileName);
			computerApplyPojo.setALSignDate(new Date());
			break;
		case 3:
			computerApplyPojo.setReviewStaffSignImgUrl(savedFileName);
			computerApplyPojo.setReviewStaffSignDate(new Date());
		case 4:
			computerApplyPojo.setITLeaderSignImgUrl(savedFileName);
			computerApplyPojo.setITLeaderSignDate(new Date());
			break;
		case 5:
			computerApplyPojo.setProcurementManagerSignImgUrl(savedFileName);
			computerApplyPojo.setProcurementManagerSignDate(new Date());
			break;
		case 6:
			computerApplyPojo.setProcurementLeaderSignImgUrl(savedFileName);
			computerApplyPojo.setProcurementLeaderSignDate(new Date());
		default:
			ajaxJson.data = "程序有问题，请联系管理员";
			ajaxJson.code = AjaxJson.error;
			return ajaxJson;
		}
		@Cleanup
		FileOutputStream fos = new FileOutputStream(pathname);
		fos.write(b64Png);
		fos.flush();
		computerApplyService.updateComputerApplyById(computerApplyPojo);
		return ajaxJson;
	}

}
